
import XRequest from '@/library/XRequest.js'

const $request = new XRequest();
$request.config.baseUrl = "http://localhost:3000/";
$request.config.isJson = true;

$request.config.headers = {
    'content-type':'application/json'
};

$request.interceptor.response.convert = (xhr) =>{
    const contentType = xhr.getResponseHeader('content-type');
    // contentType => application/json;
    if(contentType.includes('application/json')){
        return JSON.parse(xhr.responseText);
    }
    return xhr.responseText;
}

$request.interceptor.response.next = (res)=>{
    switch (res.code){
        case 300:
            console.log(res.message)
            break;
        case 400:
            // 未登录
            // 跳转登录
            break;
    }
}

$request.interceptor.request = (init) =>{
    let headers = init.headers ?? {};
    headers = {
        ...headers,
    }
    init.headers = headers;
    return init;
}

export default $request;